import BaseRedTipManager from "./BaseRedTipManager";
import RedTipInfo from "./RedTipInfo";
import { RedTipPos } from "./RedTipConst";
import RedTipAble from "../interface/RedTipAble";
// import BaseItemView from "../base/view/BaseItemView";


export default class ItemTipManager extends BaseRedTipManager{


    private itemMap =  {};

    /**
     * 
     * @param layerName 
     * @param widgetName 
     * @param redType 
     * @param id 道具的数据库id
     */
    add( layerName,widgetName,redType,id){
        // cc.log("ItemTipManager add layerName ",layerName," widgetName ",widgetName," id ",id); 
        if(!this.itemMap[layerName]){
            this.itemMap[layerName] = {};
        }
        if(!this.itemMap[layerName][widgetName]){
            this.itemMap[layerName][widgetName] = {};
        }

        if(!this.itemMap[layerName][widgetName][id]){
            this.itemMap[layerName][widgetName][id] = new RedTipInfo(id);
        }

        return this.itemMap[layerName][widgetName][id].add(redType);
    }

    /**
     * 
     * @param layerName 
     * @param widgetName 
     * @param redType 
     * @param id 
     */
    delete( layerName,widgetName,redType ,id){
        if(!this.itemMap[layerName]){
            return false;
        }
        if(!this.itemMap[layerName][widgetName]){
            return false;
        }

        if(!this.itemMap[layerName][widgetName][id]){
            return false
        }
        return this.itemMap[layerName][widgetName][id].delete(redType);        
    }


    /**
     * 
     * @param layerName 
     * @param widgetName 
     * @param id 
     */
    getWidgetInfo(layerName,widgetName,id){
        if(!this.itemMap[layerName]){
            return null;
        }
        if(!this.itemMap[layerName][widgetName]){
            return null;
        }

        if(!this.itemMap[layerName][widgetName][id]){
            return null
        }
        return this.itemMap[layerName][widgetName][id];          
    }

    deleteOtherRedTip( layer:RedTipAble,widgetVector,id ){
        let layerName = layer.getRedTipName()
        for (let index = 0; index < widgetVector.length; index++) {
            const data = widgetVector[index];
            let widgetName = data[RedTipPos.widgetPath]
            if(!widgetName){
                widgetName = layerName;
            }
            let widgetInfo: RedTipInfo = this.getWidgetInfo(layerName,widgetName,id);
            if( widgetInfo && widgetInfo.getCount() <= 0){
                let widget = cc.find(widgetName,layer.getRedTipNode());
                if(widget){
                    this.deleteRedSprite(widget);
                }
            }
        }       
    }
    

    deleteRedTip( layer:RedTipAble,widgetVector,id ){
        let layerName = layer.getRedTipName()
        // cc.log("deleteRedTip layerName "+layerName);
        if(widgetVector){
            for (let index = 0; index < widgetVector.length; index++) {
                const data = widgetVector[index];
                let widgetName = data[RedTipPos.widgetPath]
                if(!widgetName){
                    widgetName = layerName;
                }
                let widgetInfo: RedTipInfo = this.getWidgetInfo(layerName,widgetName,id);
                if( widgetInfo && widgetInfo.getCount() <= 0){
                    let widget = cc.find(widgetName,layer.getRedTipNode());
                    if(!widget){
                        widget = layer.getRedTipNode();
                    }
                    if(widget){
                        this.deleteRedSprite(widget);
                    }
                }
            }
        }       
    }


    addRedTip( layer,widgetVector,id ){
        let layerName = layer.node.name;
        // cc.log("addRedTip layerName "+layerName);
        if(widgetVector){
            for (let index = 0; index < widgetVector.length; index++) {
                const data = widgetVector[index];
                let widgetName = data[RedTipPos.widgetPath]
                if(!widgetName){
                    widgetName = layerName;
                }
                let widgetInfo: RedTipInfo = this.getWidgetInfo(layerName,widgetName,id);
                // cc.log(" widgetInfo.getCount() ",widgetInfo.getCount());
                if( widgetInfo && widgetInfo.getCount() > 0){
                    let widget = cc.find(widgetName,layer.node);
                    if(!widget){
                        widget = layer.node;
                       
                    }
                    if(widget){
                        this.addRedSprite(widget,data);
                    }else{
                        cc.log("widget is nul layerName  widgetName  ",layerName,widgetName);
                    }
                }
            }
        }      
    }

}
